﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;

namespace Client
{
    public partial class FormVipPayAudit :BaseChildForm
    {
        QunYing.BLL.VipInfo bll_VipInfo = new QunYing.BLL.VipInfo();
        QunYing.BLL.VipPayOrder bll_VipPayOrder = new QunYing.BLL.VipPayOrder();
        public FormVipPayAudit()
        {
            InitializeComponent();
            this.BindData();
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            if (!this.DesignMode && this.panel_btn != null)
            {
                if (this.panel_btn.Parent != null)
                    this.panel_btn.Left = (this.panel_btn.Parent.Width - this.panel_btn.Width) / 2;
            }
            base.OnSizeChanged(e);
        }

        public override void BindData()
        {
            DataTable dt = this.bll_VipPayOrder.GetList(string.Format("审核状态='{0}'", "未审核")).Tables[0];
            this.sharpGrid1.DataSource = dt;

            //base.BindData();
        }

        private void btn_审核通过_Click(object sender, EventArgs e)
        {
            try
            {
                GridView view = this.sharpGrid1.MainView as GridView;
                DataRow row = GetSelectedRow();
                if (row == null)
                {
                    GlobalInfo.MsgBoxShowError("请选择一个审核单！");
                    return;
                }
                DataTable dt = row.Table.Clone();
                dt.ImportRow(row);
                var v = bll_VipPayOrder.DataTableToList(dt)[0];
                v.审核状态 = "审核通过";
                v.审核人 = GlobalInfo.UserLoginInfo.用户ID;
                v.审核时间 = DateTime.Now;
                bll_VipPayOrder.Update(v);
                var vv= bll_VipInfo.GetModel(v.会员卡号);
                vv.审核状态 = "审核通过";
                vv.到期时间 = v.新到期时间;
                vv.会员级别 = v.会员新级别;
                vv.所选服务项 = v.新服务选项;
                bll_VipInfo.Update(vv);
                GlobalInfo.MsgBoxShowInfo(string.Format("单据号：{0}   审核工作已完成！" ,v.缴费单ID));
                this.BindData();
            }
            catch (Exception ex)
            {
                GlobalInfo.MsgBoxShowError("审核用户续费信息出现错误："+ex.Message);
            }
        }

        private DataRow GetSelectedRow()
        {
            GridView view = this.sharpGrid1.MainView as GridView;
            int[] rows = view.GetSelectedRows();
            if (rows.Length == 0)
                return null;
            foreach (var v in rows)
            {
                if (v < 0)
                    continue;
                DataRowView rowView = view.GetRow(v) as DataRowView;
                if (rowView == null)
                    return null;
                return rowView.Row;
            }
            return null;
        }

        private void btn_不予通过_Click(object sender, EventArgs e)
        {
            try
            {
                GridView view = this.sharpGrid1.MainView as GridView;
                DataRow row = GetSelectedRow();
                if (row == null)
                {
                    GlobalInfo.MsgBoxShowError("请选择一个审核单！");
                    return;
                }
                DataTable dt = row.Table.Clone();
                dt.ImportRow(row);
                var v = bll_VipPayOrder.DataTableToList(dt)[0];
                v.审核状态 = "审核不通过";
                v.审核人 = GlobalInfo.UserLoginInfo.用户ID;
                v.审核时间 = DateTime.Now;
                bll_VipPayOrder.Update(v);
                GlobalInfo.MsgBoxShowInfo(string.Format("单据号：{0}   审核工作已完成！", v.缴费单ID));
                this.BindData();
            }
            catch (Exception ex)
            {
                GlobalInfo.MsgBoxShowError("审核用户续费信息出现错误：" + ex.Message);
            }
        }
    }
}